<?php
	class wrapper
	{
		var $_table;
		var $_cle_primaire;
		var $_jointures = array();
		var $_db;
		
		function __construct()
		{
			$this -> _db = GP_db::getInstance(BDD_DSN, BDD_UTILISATEUR, BDD_MOT_DE_PASSE);
		}
		
		function settable($table)
		{
			if (empty($this -> _table))
			{
				$this -> _table = $table;
				$q = "DESCRIBE " . $table;
				$res = $this -> _db -> fetchArray($q);
				$pri = false;
				foreach ($res as $f => $tab)
				{
					foreach ($tab as $c => $v)
					{
						if ($c == 'Key' && $v == 'PRI')
						{
							$this -> _cle_primaire = $tab["Field"];
							$pri = true;
							break;
						}
					}
					if ($pri)
						break;
				}
			}
		}
		
		function setjointure($tablejointe, $condition, $type = 'LEFT JOIN')
		{
			if (empty($this -> _jointures[$tablejointe]))
				$this -> _jointures[$tablejointe] = $type . ' ' . $tablejointe . ' ON ' . $conditions;
		}
		
		function insert($ins)
		{
			if (strlen($this -> _table) > 0 && is_array($ins) && count($ins))
			{
				$tableinsert = $this -> _table;
				$q = "INSERT INTO $tableinsert SET ";
				foreach ($ins as $champs => $valeur)
					$q .= $champ . " = '" . addslashes($valeur) . "', ";
				
				$q = substr($q, 0, -2);
				
				$this -> _db -> requete($q);
				return true;
			}
			return false;
		}
		
		function update($maj, $condition = '')
		{
			if (strlen($this -> _table) > 0 && is_array($maj) && count($maj))
			{
				$tablemaj = $this -> _table;
				$q = "UPDATE $tablemaj SET ";
				foreach ($maj as $champs => $valeur)
					$q .= $champ . " = '" . addslashes($valeur) . "', ";
				
				$q = substr($q, 0, -2);
				
				if (count($this -> _jointures))
				{
					foreach ($this -> _jointures as $tablj => $jointure)
						$q .= ' ' . $jointure;
				}
				
				if (strlen($condition))
					$q .= ' WHERE ' . $condition;
				
				$this -> _db -> requete($q);
				return true;
			}
			return false;
		}
		
		function updateid($maj, $id)
		{
			return $this -> update($maj, $this -> _cle_primaire . " = '" . addslashes($id) . "'");
		}
		
		function delete($condition = '')
		{
			if (strlen($this -> _table) > 0)
			{
				$tabledel = $this -> _table;
				$q = "DELETE FROM $tabledel";
				
				if (count($this -> _jointures))
				{
					foreach ($this -> _jointures as $tablj => $jointure)
						$q .= ' ' . $jointure;
				}
				if (strlen($condition))
					$q .= ' WHERE ' . $condition;
				
				$this -> _db -> requete($q);
				return true;
			}
			return false;
		}
		
		function deleteid($id)
		{
			return $this -> delete($this -> _cle_primaire . " = '" . addslashes($id) . "'");
		}
		
		function count($condition = '')
		{
			if (strlen($this -> _table) > 0)
			{
				$tablecount = $this -> _table;
				$q = "SELECT COUNT(*) AS nb FROM $tablecount";
				
				if (count($this -> _jointures))
				{
					foreach ($this -> _jointures as $tablj => $jointure)
						$q .= ' ' . $jointure;
				}
				if (strlen($condition))
					$q .= ' WHERE ' . $condition;
				
				$res = $this -> _db -> fetchArray_one($q);
				return $res["nb"];
			}
			return 0;
		}
		
		function select($champs = '', $condition = '')
		{
			if (strlen($this -> _table) > 0)
			{
				$tableselect = $this -> _table;
				if (!is_array($champs))
				{
					if (!strlen($champs))
						$champs = '*';
					
					$q = "SELECT $champs FROM $tableselect";
					
					if (count($this -> _jointures))
					{
						foreach ($this -> _jointures as $tablj => $jointure)
							$q .= ' ' . $jointure;
					}
					if (strlen($condition))
						$q .= ' WHERE ' . $condition;
				}
				else
				{
					$q = "SELECT " . implode(', ', $champs) . " FROM $tableselect";
					
					if (count($this -> _jointures))
					{
						foreach ($this -> _jointures as $tablj => $jointure)
							$q .= ' ' . $jointure;
					}
					if (strlen($condition))
						$q .= ' WHERE ' . $condition;
				}
				return $this -> _db -> fetchArray($q);
			}
			return false;
		}
		
		function selectid($id, $champs = '')
		{
			if (!strlen($champs) && !is_array($champs))
				$champs = '*';
			return $this -> select($champs, $this -> _cle_primaire . " = '" . addslashes($id) . "'");
		}
		
		function sum($champs, $condition = '')
		{
			if (strlen($this -> _table) > 0)
			{
				$tablesum = $this -> _table;
				if (!is_array($champs))
				{
					$q = "SELECT SUM($champs) AS somme FROM $tablesum";
					
					if (count($this -> _jointures))
					{
						foreach ($this -> _jointures as $tablj => $jointure)
							$q .= ' ' . $jointure;
					}
					if (strlen($condition))
						$q .= ' WHERE ' . $condition;
					
					$res = $this -> _db -> fetchArray_one($q);
					return $res["somme"];
				}
				else
				{
					$sumfileds = array();
					foreach ($champs as $champ => $alias)
						$sumfiels[] = "SUM($champ) AS $alias";
					$q = "SELECT " . implode(', ', $sumfiels) . " FROM $tablesum";
					
					if (count($this -> _jointures))
					{
						foreach ($this -> _jointures as $tablj => $jointure)
							$q .= ' ' . $jointure;
					}
					if (strlen($condition))
						$q .= ' WHERE ' . $condition;
					
					return $this -> _db -> fetchArray_one($q);
				}
			}
			return false;
		}
		
		function sumid($champs, $id)
		{
			return $this -> sum($champs, $this -> _cle_primaire . " = '" . addslashes($id) . "'");
		}
		
		function LastId()
		{
			return $this -> _db -> LastId();
		}
	}
?>